library(dplyr)

# PATIENT METADATA MUST BE OBTAINED FROM EGA DATASET EGAS00001005450
data <- read.csv("data_merged.csv", stringsAsFactors = T)

# Note: Missing data coerced to NAs when coenverting character to numeric values
suppressWarnings({
  data <- data %>%
    # Convert selected columns to numeric.
    mutate_at(vars(
      "cigqty", "cigdur", "cigagestart", "cigagestop", "cigpackyears",
      "alcqty", "alcdur", "alcagestop", "bmi"
    ), function(x) as.numeric(as.character(x))) %>%
    mutate(
      # Categorize age
      age_group = case_when(
        age_diag <= 45 ~ "0-45", age_diag > 45 & age_diag <= 55 ~ "45-55",
        age_diag > 55 & age_diag <= 65 ~ "55-65",
        age_diag > 65 & age_diag <= 75 ~ "65-75", age_diag > 75 ~ "75+"
      ),
      # Create binary variables for subsites
      OC = ifelse(subsite == "OC", "yes", "no"),
      OPC = ifelse(subsite == "OPC", "yes", "no"),
      Larynx = ifelse(subsite == "Larynx", "yes", "no"),
      Hypopharynx = ifelse(subsite == "Hypopharynx", "yes", "no"),
      #  Categorize region
      region = ifelse(country %in% c("Greece", "Italy", "Czech Republic", "Slovakia", "Romania"), "Europe", "SouthAmerica"),
      # Create additional variables for smoking and drinking
      tobacco_ever = ifelse(tobacco == "Never", "No", "Yes"),
      alcohol_ever = ifelse(alcohol == "Never", "Non-drinker", "Drinker"),
      tob_alc = case_when(
        tobacco != "Never" & alcohol != "Never" ~ "Tobacco + alcohol",
        tobacco != "Never" & alcohol == "Never" ~ "Tobacco",
        tobacco == "Never" & alcohol != "Never" ~ "Alcohol",
        tobacco == "Never" & alcohol == "Never" ~ "None"
      ),
      # HPV status
      hpv_pos = case_when(hpv_sero == "Missing" ~ "Negative", TRUE ~ hpv_sero),
      hpv_pos_opc = case_when(subsite == "OPC" ~ hpv_pos, TRUE ~ "Missing"),
      #  Categorize additional risk factors
      bmi_cat = ifelse(bmi >= median(bmi, na.rm = T), "above_median", "below_median"),
      oral_cat = case_when(
        oral_score == 1 | oral_score == 2 ~ "low",
        oral_score == 3 | oral_score == 4 ~ "high"
      ),
      hot_tea = case_when(
        teatemp == "Hot" | teatemp == "Very hot" ~ "Yes",
        teatemp == "Warm" | teatemp == "Not applicable" ~ "No",
        teatemp == "Missing" ~ "Missing"
      ),
      coffeestatus_cat = case_when(
        coffeestatus == "Ex-drinker" | coffeestatus == "Ever drinker" ~ "Ever drinker",
        coffeestatus == "Never" ~ "Never"
      ),
      hot_coffee = case_when(
        coffeetemp == "Hot" | coffeetemp == "Very hot" ~ "Yes",
        coffeetemp == "Warm" | coffeetemp == "Not applicable" ~ "No",
        coffeetemp == "Missing" ~ "Missing"
      ),
      matestatus_cat = case_when(
        matestatus == "Ex-drinker" | matestatus == "Current drinker" ~ "Ever drinker",
        matestatus == "Never" ~ "Never"
      ),
      hot_mate = case_when(
        matetemp == "Hot" | matetemp == "Very hot" ~ "Yes",
        matetemp == "Warm" | matetemp == "Cold" | matetemp == "Not applicable" ~ "No",
        matetemp == "Missing" ~ "Missing"
      ),
      hotdrinks = case_when(
        hot_tea == "Yes" | hot_coffee == "Yes" | hot_mate == "Yes" ~ "Yes",
        hot_tea == "No" & hot_coffee == "No" ~ "No",
        TRUE ~ "Missing"
      ),
      # Order levels
      sex = factor(sex, levels = c("Male", "Female")),
      country = factor(country, levels = c("Colombia", "Argentina", "Greece", "Italy", "Brazil", "Czech Republic", "Slovakia", "Romania")),
      region = factor(region, levels = c("SouthAmerica", "Europe")),
      stage = factor(stage, levels = c("I", "II", "III", "IV")),
      subsite = factor(subsite, levels = c("OC", "OPC", "Hypopharynx", "Larynx")),
      tobacco = factor(tobacco, levels = c("Never", "Ex-smoker", "Current smoker")),
      alcohol = factor(alcohol, levels = c("Never", "Ex-drinker", "Ever drinker", "Current drinker")),
      alcohol_ever = factor(alcohol_ever, c("Non-drinker", "Drinker")),
      tob_alc = factor(tob_alc, levels = c("None", "Alcohol", "Tobacco", "Tobacco + alcohol"))
    ) %>%
    mutate_at(
      vars("tobacco_ever", "alcohol_ever", "bmi_cat", "oral_cat", "hotdrinks"),
      function(x) as.factor(x)
    )
})

data[data == "Missing"] <- NA
